//
//  SinaTabBarController.swift
//  MySinaWeiBoSwiftProject
//
//  Created by apple on 2016/11/12.
//  Copyright © 2016年 WangChen. All rights reserved.
//

import UIKit

class SinaTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
        //添加naviVC
        addNaviControllers()
        
        //KVC使用自定义TabBar
        let myTabbar = SinaTabBarView()
        
        myTabbar.composeBtnDidClick = {
            [weak self] in
            
            print("撰写按钮被点击了!")
            let composeVC = SinaHomeComposeView()
            composeVC.show(fromVc: self)
            
        }
        
        setValue(myTabbar, forKey: "tabBar")
        
       
    }

    
    // MARK:
    // MARK: - 添加NaviController
    private func addNaviControllers(){
        
        addNaviController(vc: SinaHomeViewController(), title: "主页", imageName: "tabbar_home", index: 0)
        addNaviController(vc: SinaMessageViewController(), title: "消息", imageName: "tabbar_message_center",index: 1)
        addNaviController(vc: SinaDiscoverViewController(), title: "发现", imageName: "tabbar_discover",index: 2)
        addNaviController(vc: SinaProfileViewController(), title: "我的", imageName: "tabbar_profile",index: 3)
        
    }
    
    
    // MARK:
    // MARK: - 统一的添加NaviController的方法
    private func addNaviController(vc: UIViewController,title: String,imageName: String,badgeValue: String? = nil, index: Int){
    
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)?.withRenderingMode(.alwaysOriginal)
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
        
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: .selected)
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFont(ofSize: 10)], for: .normal)
        
        vc.tabBarItem.titlePositionAdjustment = UIOffset.init(horizontal: 0, vertical: -3)
        
        //设置badge属性
        vc.tabBarItem.badgeColor = #colorLiteral(red: 0.3647058904, green: 0.06666667014, blue: 0.9686274529, alpha: 1)
        vc.tabBarItem.badgeValue = badgeValue
        vc.tabBarItem.tag = index
        
        
        let Navi = SinaNaviBaseController(rootViewController: vc)
        
        addChildViewController(Navi)
        
    }

    
    // MARK:
    // MARK: - 重写didselect方法 给按钮加动画
    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        
        var index = 0
        for subView in tabBar.subviews{
            if subView.isKind(of: NSClassFromString("UITabBarButton")!) {
                if index == item.tag{
                    for v in subView.subviews{
                        if v.isKind(of: UIImageView.self){
                            v.transform = CGAffineTransform.init(scaleX: 0.7, y: 0.7)
                            UIView.animate(withDuration: 0.7, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 6, options: [], animations: { 
                                v.transform = CGAffineTransform.identity
                            }, completion: { (_) in
                                print("👌")
                            })
                            
                        }
                    }
                }
                index += 1
            }
        }
        
    }
    
    
    
    

}
